{% set periods = guest.system_periods %}
{% set pricing = product.pricing %}
<section>
            <fieldset>
                <div class="control-group">
                    <label>{% trans 'Register a new domain' %}</label>
                    <div class="form-horizontal">
                        <input type="text" name="register_sld" class="span3" value="{{ request.register_sld }}" placeholder="{% trans 'Enter new domain name to register' %}">
                        {% set tlds = guest.serviceDomain_tlds({"allow_register":1})%}
                        <select name="register_tld" class="span2">
                            {% for tld in tlds%}
                            <option value="{{ tld.tld}}" label="{{ tld.tld}}">{{ tld.tld}}</option>
                            {% endfor %}
                        </select>
                        <button class="btn btn-inverse" type="button" id="domain-check">{% trans 'Check' %}</button>
                    </div>
                </div>
                <div class="control-group">
                <div class="onAfterDomainCheck" style="display:none;">
                    <label>
                        <select name="register_years" id="registration-years"></select>
                    </label>
                    <br/>
                    <label class="checkbox"><input type="checkbox" onclick="$('#nameservers').slideToggle();">{% trans 'I want to use my nameservers' %}</label>
                    <div id="nameservers" style="display:none; margin: 10px 0;">
                        <div class="row-fluid">
                            <input type="text" name="ns1" value="" placeholder="{% trans 'Nameserver 1' %}"/>
                        </div>
                        <div class="row-fluid">
                            <input type="text" name="ns2" value="" placeholder="{% trans 'Nameserver 2' %}"/>
                        </div>
                        <div class="row-fluid">
                            <input type="text" name="ns3" value="" placeholder="{% trans 'Nameserver 3' %}"/>
                        </div>
                        <div class="row-fluid">
                            <input type="text" name="ns4" value="" placeholder="{% trans 'Nameserver 4' %}"/>
                        </div>
                    </div>

                </div>
                </div>
            </fieldset>
</section>
<input type="hidden" name="id" value="{{ product.id }}" />
<input type="hidden" name="action" value="register" id="domain-action"/>

{% set currency = guest.cart_get_currency %}
<script type="text/javascript">
$(function() {

    $('ul.tabs li a').bind('click', function (){
        $('#domain-action').val($(this).attr('rel'));

        $(this).parents('ul').find('span.dark-icon')
            .removeClass('i-check i-uncheck')
            .addClass('i-uncheck');

        $(this).find('span')
            .removeClass('i-uncheck')
            .addClass('i-check');
    });
    $('ul.tabs li a:first').click();

    if($(".addons").length) {
        $('.order-button').one('click', function(){
            $(this).hide();
            $('.addons').slideDown('fast');
            return false;
        });
    }

    $('.addon-period-selector').change(function(){
        var r = $(this).attr('rel');
        $('#' + r + ' span').hide();
        $('#' + r + ' span.' + $(this).val()).fadeIn();
    }).trigger('change');

    $('#transfer-check').bind('click',function(event){
        var sld = $('input[name="transfer_sld"]').val();
        var tld = $('select[name="transfer_tld"]').val();
        var domain = sld + tld;
        bb.post(
            'guest/servicedomain/can_be_transferred',
            { sld: sld, tld: tld },
            function(result) {
                setTransferPricing(tld);
                $('#domain-name').text(domain);
                $('#domain-transfer-config').fadeIn('fast');
                $('.onAfterDomainCheck').fadeIn('fast');
                $('#transfer-check').hide();
                $('#transfer .order-button').show();
            }
        );
        return false;
    });

    $('#domain-check').bind('click',function(event){
        var sld = $('input[name="register_sld"]').val();
        var tld = $('select[name="register_tld"]').val();
        var domain = sld + tld;
        bb.post(
            'guest/servicedomain/check',
            { sld: sld, tld: tld },
            function(result) {
                setPricing(tld);
                $('#domain-name').text(domain);
                $('.onAfterDomainCheck').fadeIn('fast');
                $('#domain-check').hide();
                $('#register .order-button').show();
            }
        );
        return false;
    });

    function setPricing(tld)
    {
        bb.post(
            'guest/servicedomain/pricing',
            { tld: tld },
            function(result) {
                var s = $("#registration-years");
                s.find('option').remove();
                for (i=1;i<6;i++) {
                    var price = bb.currency(result.price_registration, {{ currency.conversion_rate }}, "{{ currency.code }}", i);
                    s.append(new Option(i + "{% trans ' Year/s @ ' %}" + price, i));
                }
            }
        );
    }

    function setTransferPricing(tld)
    {
        bb.post(
            'guest/servicedomain/pricing',
            { tld: tld },
            function(result) {
                var price = bb.currency(result.price_transfer, {{ currency.conversion_rate }}, "{{ currency.code }}");
                $('#transfer-price').text(price);
            }
        );
    }
});
</script>
